Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
what is the meaning of onsubmit="return false" ? why onsubmit="false" not working? what is the difference between them?

formValidation() function return false but not preventing form submission

<body>
    <script>
        function formValidation(){
            return false;
        }
    </script>
    <form onsubmit="formValidation()" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
        <label for="email">Email:</label><br>
        <input type="email" name="email" id="email"><br><br>
        <input type="submit" name="submit" value="submit">
    </form>
</body>

When i have used return front of formValidation() function it is working

<body>
    <script>
        function formValidation(){
            return false;
        }
    </script>
    <form onsubmit="return formValidation()" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
        <label for="email">Email:</label><br>
        <input type="email" name="email" id="email"><br><br>
        <input type="submit" name="submit" value="submit">
    </form>
</body>
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Inline handlers like these essentially turn into a function body. Said function is executed when the event is fired.

function theHandler() {
  // Attribute string goes here
}

If the function returns false, the event's default action (such as submitting the form) is prevented.

<form onsubmit="formValidation()"

doesn't work because then the constructed function that gets executed doesn't return anything:

function theHandler() {
  formValidation()
}

But

<form onsubmit="return formValidation()"

works because then the constructed function is like

function theHandler() {
  return formValidation()
}

It's somewhat confusing - and is one of the (many) reasons why you should never use inline handlers. Attach the event listener properly using JavaScript instead.

document.querySelector('form').addEventListener('submit', formValidation);
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!